
package com.kanjian.music.activity;

import java.util.ArrayList;

import com.kanjian.music.R;
import com.kanjian.music.adapter.MusicianListListAdapater;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.network.HttpLoader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.ListView;

public class MyFollowMusicianActivity extends BaseActivity implements LoaderCallbacks<String> {

    private ListView mLv_musician;

    private MusicianListListAdapater mListAdapter;

    private ArrayList<Musician> mMusicianList;

    private LocalBroadcastManager mLocalBroadcastManager;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_myfollow_musician);
        super.onCreate(arg0);
        initView();
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getFollowRequest(0));
        getSupportLoaderManager().restartLoader(0, args, this);
        registerBroadcastReceiver();
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction() != null) {
                int musicianId = intent.getIntExtra(IntentConstants.MUSICIANID, -1);
                if (intent.getAction().equalsIgnoreCase(
                        IntentConstants.ACTION_CANCEL_FOLLOW_MUSICIAN)) {

                    if (mMusicianList != null) {
                        for (int i = 0; i < mMusicianList.size(); i++) {
                            if (mMusicianList.get(i).mUserId == musicianId) {
                                mMusicianList.remove(i);
                                if (mListAdapter != null) {
                                    mListAdapter.setMusicList(mMusicianList);
                                    mListAdapter.notifyDataSetChanged();
                                }
                                break;
                            }
                        }
                    }

                } else if (intent.getAction().equalsIgnoreCase(
                        IntentConstants.ACTION_DO_FOLLOW_MUSICIAN)) {

                }
            }
        }
    };

    private void registerBroadcastReceiver() {
        if (mLocalBroadcastManager == null) {
            mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(IntentConstants.ACTION_CANCEL_FOLLOW_MUSICIAN);
        filter.addAction(IntentConstants.ACTION_DO_FOLLOW_MUSICIAN);
        mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, filter);
    }

    private void unregisterBroadcastReceiver() {
        mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterBroadcastReceiver();
    }

    private void initView() {
        mLv_musician = (ListView) findViewById(R.id.musicianlist_listview);
        mListAdapter = new MusicianListListAdapater(this, mLv_musician);
        mLv_musician.setAdapter(mListAdapter);
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(this, args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            mMusicianList = Musician.getMusicianListFromJson(data);
            if (mMusicianList != null) {
                mListAdapter.setMusicList(mMusicianList);
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
